package com.yanggeng.demo.beginnerguidedemo.activity.customview;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.SwitchCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.activity.BaseActivity;
import com.yanggeng.demo.beginnerguidedemo.utils.SnackbarUtil;
import com.yanggeng.demo.beginnerguidedemo.utils.ValidateUtils;

import static android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
import static android.view.inputmethod.EditorInfo.IME_ACTION_NEXT;

/**
 * Created by yanggeng on 2017/7/11.
 */

public class TextInputLayoutActivity extends BaseActivity {

    TextInputLayout til_mobile_phone, til_password;
    EditText et_mobile_phone, et_password;
    View main_view;
    SwitchCompat switchCompat;
    FloatingActionButton fab;
    ProgressDialog progressDialog;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 100) {
                progressDialog.dismiss();
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_textinputlayout);
        setTitle(true, "输入框");
        main_view = findViewById(R.id.main_view);
        til_mobile_phone = (TextInputLayout) findViewById(R.id.til_username);
        til_password = (TextInputLayout) findViewById(R.id.til_password);
        findViewById(R.id.btn_login).setOnClickListener(this);
        et_mobile_phone = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        switchCompat = (SwitchCompat) findViewById(R.id.switch_voice);
        switchCompat.setShowText(false);
//        switchCompat.setTextOff("off");
//        switchCompat.setTextOn("on");
        switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    SnackbarUtil.shortSnackbar(main_view, "on");
                } else {
                    SnackbarUtil.shortSnackbar(main_view, "off");
                }
            }
        });

        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setRippleColor(Color.parseColor("#7fffffff"));

        et_mobile_phone.requestFocus();
        et_mobile_phone.addTextChangedListener(new MobilePhoneTextWatcher());
        et_password.addTextChangedListener(new PasswordTextWatcher());

        et_mobile_phone.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == IME_ACTION_NEXT) {
                    if(!ValidateUtils.isMobilePhone(et_mobile_phone.getText().toString().trim())) {
                        til_mobile_phone.setError("Invalid Mobile Phone");
                    }
                    et_password.requestFocus();
                    return true;
                }
                return false;
            }
        });
        et_password.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == IME_ACTION_DONE) {
                    toLogin();
                    return true;
                }
                return false;
            }
        });

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if(v.getId() == R.id.btn_login) {
            toLogin();
        }
    }

    private void showDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }
        progressDialog.setMessage("请稍后...");
        progressDialog.setCancelable(true);
        progressDialog.show();
    }

    private void toLogin() {
        if(!ValidateUtils.isMobilePhone(et_mobile_phone.getText().toString().trim())) {
            til_mobile_phone.setError("Invalid Mobile Phone");
        }
        else if(et_password.length() < 6 || et_password.length() > 16 || !ValidateUtils.isPassword(et_password.getText().toString().trim())) {
            til_password.setError("Invalid Password");
        } else {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
            SnackbarUtil.shortSnackbar(main_view, "正在登录...");
            showDialog();
            handler.sendEmptyMessageDelayed(100, 2000);
        }
    }

    class MobilePhoneTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(s.length() == 11 && !ValidateUtils.isMobilePhone(s.toString())) {
                til_mobile_phone.setError("Invalid Mobile Phone");
            } else {
                til_mobile_phone.setError(null);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    class PasswordTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if(!ValidateUtils.isPassword(s.toString().trim())) {
                til_password.setError("Invalid Password");
            } else {
                til_password.setError(null);
            }
        }
    }
}
